

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.PersonDAO;
import db.ProjectDAO;
import db.ResourceDAO;
import db.SkillDAO;

/**
 * Servlet implementation class AdminServlet
 */
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("skills", SkillDAO.getSkills());
		request.setAttribute("resources", ResourceDAO.getResources());
		request.setAttribute("resourceSchedules", ResourceDAO.getResourceSchedules());
		request.setAttribute("people", PersonDAO.getPeople());
		request.setAttribute("projects", ProjectDAO.getProjectsAndManagers());
		getServletContext().getRequestDispatcher("/admin-home.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("act");
		req.getSession().setAttribute("msg", "action: " + action);
		System.out.println("action: " + action);
		if ("addUser".equals(action)) {
			PersonDAO.addUser(req.getParameter("email"), req.getParameter("role"));
			doGet(req, resp);
		} else if ("addProject".equals(action)) {
			String errorMsg = ProjectDAO.addProject(req.getParameter("title"), req.getParameter("pm_id"));
			if (errorMsg != null && !"".equals(errorMsg)) {
				req.setAttribute("msg", errorMsg);
			}
			doGet(req, resp);
		} else if ("addResource".equals(action)) {
			String errorMsg = ResourceDAO.addResource(req.getParameter("description"));
			if (errorMsg != null && !"".equals(errorMsg)) {
				req.setAttribute("msg", errorMsg);
			}
			doGet(req, resp);
		} else if ("addSkill".equals(action)) {
			String errorMsg = SkillDAO.addSkill(req.getParameter("description"));
			if (errorMsg != null && !"".equals(errorMsg)) {
				req.setAttribute("msg", errorMsg);
			}
			doGet(req, resp);
		} else if ("assignPersonProject_one".equals(action)) {
			int person_id = Integer.parseInt(req.getParameter("person_id"));
			req.setAttribute("projects", ProjectDAO.getProjectsAndManagers());
			req.setAttribute("person", PersonDAO.getPerson(person_id));
			getServletContext().getRequestDispatcher("/admin-proj.jsp").forward(req, resp);
		} else if ("assignPersonProject_two".equals(action)) {
			int personId = Integer.parseInt(req.getParameter("person_id"));
			try {
				String sd = req.getParameter("startDate");
				String ed = req.getParameter("endDate");
				int p  = Integer.parseInt(req.getParameter("percent"));
				int projId = Integer.parseInt(req.getParameter("proj_id"));
				PersonDAO.assignPersonToProject(personId, projId, sd, ed, p);
				doGet(req, resp);				
			} catch (Exception e) {
				e.printStackTrace();
				req.setAttribute("msg", e.getMessage());
				req.setAttribute("projects", ProjectDAO.getProjectsAndManagers());
				req.setAttribute("person", PersonDAO.getPerson(personId));
				getServletContext().getRequestDispatcher("/admin-proj.jsp").forward(req, resp);
			}
		}
	}

}
